package gcu.module.rbac.dao;

import gcu.common.domain.Page;
import gcu.module.rbac.dao.mapper.UserMapper;
import gcu.module.rbac.daomain.User;
import gcu.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by haol on 2016/9/20.
 */
@Repository
public class UserDao {

      private Logger logger = LoggerFactory.getLogger(UserDao.class);

      @Autowired
      private UserMapper mapper;

      /*根据用户名获取用户*/
      public User queryUserByName(String username) {
            try {
                  return mapper.queryUserByName(username);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }

      /*获取全部角色*/
      public List<User> queryUserList(User user, Pager pager) {
            try {
                  int skip = (pager.getCurrentPage() - 1) * pager.getPageSize();
                  int pageSize = pager.getPageSize();
                  pager.setTotalSize(mapper.count(user));
                  return mapper.query(user, skip, pageSize);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }
   /*获取全部角色*/
            public List<User> queryUserList() {
                  try {
                        return mapper.queryAll();
                  } catch (Exception e) {
                        logger.error(e.getMessage());
                        e.printStackTrace();
                        return null;
                  }
            }
    /*z增加用户*/

      public void addUser(User user) {
            try {
                  mapper.addUser(user);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }


      /*删除用户*/
      public void deleteUser(int id) {
            try {
                  mapper.deleteUser(id);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

      /*更新用户*/
      public void updateUser(User user) {
            try {
                  mapper.updateUser(user);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

      public User queryUserById(Integer id) {
            try {
                  return mapper.queryUserById(id);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }

      public void updateTime(String updateTime, String username) {
            try {
                  mapper.updateTime(updateTime, username);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }
      public void updatePwd(String username, String password) {
            try {
                  mapper.updatePwd(username, password);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }
}
